package com.qd.panda.stroke;

import com.cdqidi.dto.ApiResult;
import com.cdqidi.dto.ApiResultWrapper;
import com.qd.common.panda.constant.PandaConstant;
import com.qd.common.panda.domain.entity.stroke.PayResult;
import com.qd.panda.service.stroke.PayResultService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 接收支付和退款的状态报告
 *
 * @author sjk
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/app/userStroke/notify/")
@Slf4j
public class PayNotifyController {
    private final PayResultService payResultService;

    @RequestMapping(value = "/payAfter")
    public ApiResultWrapper<Object> payAfter(@RequestBody PayResult result) {
        //log.info("result:{},附加参数: {}", result.toString(), result.getAttach());
        if (result.getState() == 0) {
            payResultService.payOrder(result);
        }
        if (result.getState() == 1) {
            log.info("退款result: {}", result);
            String refundReason = result.getRefundReason();
            if (StringUtils.hasLength(refundReason)) {
                if (refundReason.equals(PandaConstant.REFUND_DESC_WALLET)) {
                    payResultService.refundOtherOrder(result);
                } else {
                    payResultService.refundOrder(result);
                }
            } else {
                payResultService.refundOrder(result);
            }
        }
        return ApiResult.success("ok");
    }
}
